﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.IO;

namespace OnlineShop
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        private TextBox shopName = new TextBox();
        private TextBox userName;
        private TextBox userBalance;
        private PasswordBox userPass;
        Shop theShop;
        public MainWindow()
        {
            InitializeComponent();
            InitializeStartPage();
        }

        private void InitializeStartPage()
        {
            mainGrid.Children.Clear();
            mainGrid.RowDefinitions.Clear();
            mainGrid.ColumnDefinitions.Clear();

            for (int rows = 0; rows < 4; rows++)
            {
                mainGrid.RowDefinitions.Add(new RowDefinition());
            }

            Button addUser = new Button() { Content = "Add new user", };
            Button loginUser = new Button() { Content = "Login as user" };
            Button addShop = new Button() { Content = "Add new shop" };
            Button loginShop = new Button() { Content = "Login as shop" };

            Grid.SetRow(addUser, 0);
            Grid.SetRow(loginUser, 1);
            Grid.SetRow(addShop, 2);
            Grid.SetRow(loginShop, 3);

            addUser.Click += OnAddUserClick;
            loginUser.Click += OnLoginUserClick;
            //addShop.Click += OnAddShopClick;
            loginShop.Click += OnLoginShopClick;

            mainGrid.Children.Add(addUser);
            mainGrid.Children.Add(loginUser);
            mainGrid.Children.Add(addShop);
            mainGrid.Children.Add(loginShop);

        }

        //private void ShowAddShopMenu()
        //{
        //    mainGrid.Children.Clear();
        //    mainGrid.RowDefinitions.Clear();
        //    mainGrid.ColumnDefinitions.Clear();

        //    shopName.Margin = new Thickness(125, 10, 0, 0);
        //    shopName.Width = 80;
        //    shopName.VerticalAlignment = System.Windows.VerticalAlignment.Top;
        //    shopName.HorizontalAlignment = System.Windows.HorizontalAlignment.Left;
        //    Button addShop = new Button() 
        //    { 
        //        Content = "Add", 
        //        Margin = new Thickness(10, 60, 0, 0),
        //        Height = 30,
        //        Width = 110,
        //        VerticalAlignment = System.Windows.VerticalAlignment.Top,
        //        HorizontalAlignment = System.Windows.HorizontalAlignment.Left
        //    };

        //    Button startMenu = new Button()
        //    {
        //        Content = "Go to start menu",
        //        Margin = new Thickness(10, 108, 0, 0),
        //        VerticalAlignment = System.Windows.VerticalAlignment.Top,
        //        HorizontalAlignment = System.Windows.HorizontalAlignment.Left,
        //        Height = 30,
        //        Width = 110,
        //    };
        //    startMenu.Click += OnGoStartMenuClick;
        //    //addShop.Click += OnSubmitShopClick;

        //    mainGrid.Children.Add(new TextBlock() { Text = "Name of the shop: ", Margin = new Thickness(10, 10, 0, 0) });
        //    mainGrid.Children.Add(shopName);
        //    mainGrid.Children.Add(addShop);
        //    mainGrid.Children.Add(startMenu);
        //}

        private void ShowLoginShopMenu()
        {
            mainGrid.Children.Clear();
            mainGrid.RowDefinitions.Clear();
            mainGrid.ColumnDefinitions.Clear();

            TextBlock textForName = new TextBlock()
            {
                Text = "Name: ",
                Margin = new Thickness(23, 40, 0, 0)
            };

            TextBlock passText = new TextBlock()
            {
                Text = "Password: ",
                Margin = new Thickness(23, 85, 0, 0)
            };

            TextBox name = new TextBox()
            {
                Margin = new Thickness(100, 40, 0, 0),
                Width = 150,
                Height = 25,
                HorizontalAlignment = System.Windows.HorizontalAlignment.Left,
                VerticalAlignment = System.Windows.VerticalAlignment.Top
            };

            TextBox pass = new TextBox()
            {
                Margin = new Thickness(100, 85, 0, 0),
                Width = 150,
                Height = 25,
                HorizontalAlignment = System.Windows.HorizontalAlignment.Left,
                VerticalAlignment = System.Windows.VerticalAlignment.Top
            };


            Button loginShop = new Button()
            {
                Content = "Add",
                Margin = new Thickness(20, 150, 0, 0),
                VerticalAlignment = System.Windows.VerticalAlignment.Top,
                HorizontalAlignment = System.Windows.HorizontalAlignment.Left,
                Height = 30,
                Width = 110,
            };

            Button startMenu = new Button()
            {
                Content = "Go to start menu",
                Margin = new Thickness(20, 208, 0, 0),
                VerticalAlignment = System.Windows.VerticalAlignment.Top,
                HorizontalAlignment = System.Windows.HorizontalAlignment.Left,
                Height = 30,
                Width = 110,
            };

            
            startMenu.Click += OnGoStartMenuClick;

            mainGrid.Children.Add(textForName);
            mainGrid.Children.Add(passText);
            mainGrid.Children.Add(pass);
            mainGrid.Children.Add(name);
            mainGrid.Children.Add(startMenu);
            mainGrid.Children.Add(loginShop);
        }

        private void ShowLoginUserMenu()
        {
            mainGrid.Children.Clear();
            mainGrid.RowDefinitions.Clear();
            mainGrid.ColumnDefinitions.Clear();

            TextBlock textForName = new TextBlock()
            {
                Text = "Name: ",
                Margin = new Thickness(23, 40, 0, 0)
            };

            TextBlock passText = new TextBlock()
            {
                Text = "Password: ",
                Margin = new Thickness(23, 85, 0, 0)
            };

            TextBox name = new TextBox()
            {
                Margin = new Thickness(100, 40, 0, 0),
                Width = 150,
                Height = 25,
                HorizontalAlignment = System.Windows.HorizontalAlignment.Left,
                VerticalAlignment = System.Windows.VerticalAlignment.Top
            };

            TextBox pass = new TextBox()
            {
                Margin = new Thickness(100, 85, 0, 0),
                Width = 150,
                Height = 25,
                HorizontalAlignment = System.Windows.HorizontalAlignment.Left,
                VerticalAlignment = System.Windows.VerticalAlignment.Top
            };


            Button loginShop = new Button()
            {
                Content = "Add",
                Margin = new Thickness(20, 150, 0, 0),
                VerticalAlignment = System.Windows.VerticalAlignment.Top,
                HorizontalAlignment = System.Windows.HorizontalAlignment.Left,
                Height = 30,
                Width = 110,
            };

            Button startMenu = new Button()
            {
                Content = "Go to start menu",
                Margin = new Thickness(20, 208, 0, 0),
                VerticalAlignment = System.Windows.VerticalAlignment.Top,
                HorizontalAlignment = System.Windows.HorizontalAlignment.Left,
                Height = 30,
                Width = 110,
            };


            startMenu.Click += OnGoStartMenuClick;

            mainGrid.Children.Add(textForName);
            mainGrid.Children.Add(passText);
            mainGrid.Children.Add(pass);
            mainGrid.Children.Add(name);
            mainGrid.Children.Add(startMenu);
            mainGrid.Children.Add(loginShop);
        }

        private void ShowAddUserMenu()
        {
            mainGrid.Children.Clear();
            mainGrid.RowDefinitions.Clear();
            mainGrid.ColumnDefinitions.Clear();
            Button addUserButton = new Button() 
            { 
                Content = "Add",
                Margin = new Thickness(10, 200, 0, 0), 
                Height = 30,
                Width = 110,
                HorizontalAlignment = HorizontalAlignment.Left,
                VerticalAlignment = VerticalAlignment.Top
            };
            addUserButton.Click += OnSubmitNewUserClick;

            Button addStartMenuButton = new Button()
            {
                Content = " Go to start menu ",
                Margin = new Thickness(10, 248, 0, 0),
                Height = 30,
                Width = 110,
                HorizontalAlignment = HorizontalAlignment.Left,
                VerticalAlignment = VerticalAlignment.Top
            };
            addStartMenuButton.Click += OnGoStartMenuClick;

            TextBlock nameTextBlock = new TextBlock()
            {
                Text = "Name :",
                TextWrapping = TextWrapping.Wrap,
                Margin = new Thickness(23, 43, 0, 0),
                Height = 23,
                Width = 49,
                HorizontalAlignment = HorizontalAlignment.Left,
                VerticalAlignment = VerticalAlignment.Top
            };

            TextBlock passwordTextBlock = new TextBlock()
            {
                Text = "Password :",
                TextWrapping = TextWrapping.Wrap,
                Margin = new Thickness(19, 88, 0, 0),
                Height = 23,
                Width = 63,
                HorizontalAlignment = HorizontalAlignment.Left,
                VerticalAlignment = VerticalAlignment.Top
            };

            TextBlock balanceTextBlock = new TextBlock()
            {
                Text = "Balance :",
                TextWrapping = TextWrapping.Wrap,
                Margin = new Thickness(19, 129, 0, 0),
                Height = 23,
                Width = 53,
                HorizontalAlignment = HorizontalAlignment.Left,
                VerticalAlignment = VerticalAlignment.Top
            };
            
            userName = new TextBox()
            {
                Margin = new Thickness(89, 44, 0, 0),
                Width = 140,
                HorizontalAlignment = HorizontalAlignment.Left,
                VerticalAlignment = VerticalAlignment.Top
            };
            
            userBalance = new TextBox()
            {
                Margin = new Thickness(89, 129, 0, 0),
                Width = 140,
                HorizontalAlignment = HorizontalAlignment.Left,
                VerticalAlignment = VerticalAlignment.Top
            };

            userPass = new PasswordBox()
            {
                Margin = new Thickness(89, 88, 0, 0),
                Width = 140,
                HorizontalAlignment = HorizontalAlignment.Left,
                VerticalAlignment = VerticalAlignment.Top
            };

            mainGrid.Children.Add(userName);
            mainGrid.Children.Add(userBalance);
            mainGrid.Children.Add(userPass);
            mainGrid.Children.Add(balanceTextBlock);
            mainGrid.Children.Add(passwordTextBlock);
            mainGrid.Children.Add(nameTextBlock);
            mainGrid.Children.Add(addStartMenuButton);
            mainGrid.Children.Add(addUserButton);
        }

        void OnSubmitNewUserClick(object sender, RoutedEventArgs e)
        {
            if (File.Exists("Users\\" + userName.Text + ".txt"))
            {
                MessageBox.Show(string.Format("Username {0} already exists", userName.Text));
                return;
            }
            userName.Text = userName.Text.Trim();
            if (userName.Text == string.Empty)
            {
                MessageBox.Show("Please enter valid name");
                return;
            }
            StreamWriter userWriter = new StreamWriter("Users\\" + userName.Text + ".txt");
            using (userWriter)
            {
                userWriter.WriteLine("Name: " + userName.Text);
                userWriter.WriteLine("Password: " + userPass.Password);
                userWriter.WriteLine("Balance: " + userBalance.Text);
            }
            MessageBox.Show("Registered successfully");
        }

        void OnGoStartMenuClick(object sender, RoutedEventArgs e)
        {
            InitializeStartPage();
        }

        void OnLoginUserClick(object sender, RoutedEventArgs e)
        {
            ShowLoginUserMenu();
        }

        void OnLoginShopClick(object sender, RoutedEventArgs e)
        {
            ShowLoginShopMenu();
        }

        //void OnAddShopClick(object sender, RoutedEventArgs e)
        //{
        //    ShowAddShopMenu();
        //}
        void OnAddUserClick(object sender, RoutedEventArgs e)
        {
            ShowAddUserMenu();
        }

        //private void OnSubmitShopClick(object sender, RoutedEventArgs e)
        //{
        //    //theShop = new Shop(this.shopName.Text);
        //    if (File.Exists("Shops\\" + shopName.Text + ".txt"))
        //    {
        //        MessageBox.Show(string.Format("Shop {0} already exists", shopName.Text));
        //        return;
        //    }
        //    shopName.Text = shopName.Text.Trim();
        //    if (shopName.Text == string.Empty)
        //    {
        //        MessageBox.Show(string.Format("Shop must have name"));
        //        return;
        //    }
        //    StreamWriter writer = new StreamWriter("Shops\\" + shopName.Text + ".txt");
        //    using (writer)
        //    {
        //        writer.WriteLine(theShop.Name);
        //    }
        //    MessageBox.Show("Registered successfully");
        //}

        void OnAddProductClick(object sender, RoutedEventArgs e)
        {
            StreamWriter productsWriter = new StreamWriter("Shops\\" + theShop.Name + ".txt");
            using (productsWriter)
            {
                productsWriter.WriteLine("asd");
            }
        }

    }
}
